//
//  YHMessageTableViewController.swift
//  YHWeibo
//
//  Created by 昱含 on 16/5/19.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit

class YHMessageTableViewController: YHBaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        visitorLoginView.setVisitorInfo("登录后,别人评论你的微博,发给你的消息,都会在这里收到通知", imageName: "visitordiscover_image_message")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "show", style: .Plain, target: self, action: #selector(changeValue))
        tabBarItem.badgeValue = "\(10)"
        setBackground()

    }
    
    @objc private func changeValue() {
        tabBarItem.badgeValue = nil
        tabBarItem.badgeValue = "\(random() % 100)"
    }
    private func setBackground() {
        //通过运行时获取类的成员变量
        for subview in self.tabBarController!.tabBar.subviews {
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                for v in subview.subviews {
                    if v.isKindOfClass(NSClassFromString("_UIBadgeView")!) {
                        for bgView in v.subviews {
                            if bgView.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
                                var count: UInt32 = 0
                                let list = class_copyIvarList(NSClassFromString("_UIBadgeBackground").self, &count)
                                for i in 0..<count {
                                    let ivarPoint = list[Int(i)]
                                    let ivarName = ivar_getName(ivarPoint)
                                    print(ivarName)
                                    let name = String(format: "%s",ivarName)
                                    let type = ivar_getTypeEncoding(ivarPoint)
                                    let typeName = String(format:"%s",type)
                                    print(name,typeName)
                                    let image = UIImage(named: "main_badge")
                                    bgView.setValue(image, forKey: "_image")
                                    
                                }
                                
                            }
                        }
                    }
                }
            }
            
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }


}
